package com.manager.devicemanager.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.manager.devicemanager.config.ServiceException;
import com.manager.devicemanager.entity.UserInfo;
import com.manager.devicemanager.mapper.UserInfoMapper;
import com.manager.devicemanager.service.IUserInfoService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.manager.devicemanager.util.PWUtils;
import com.manager.devicemanager.vo.UserInfoVO;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;
import java.util.Collection;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author wpf
 * @since 2025-05-17
 */
@Service
public class UserInfoServiceImpl extends ServiceImpl<UserInfoMapper, UserInfo> implements IUserInfoService {
    @Value("${user.default-password}")
    private String defaultPassword;
    @Override
    public void batchInsertOnDuplicateKeyUpdate(Collection<UserInfo> list) {
        baseMapper.batchInsertOnDuplicateKeyUpdate(list);
    }
    @Override
    public void batchInsertOnDuplicateKeyIgnore(Collection<UserInfo> list) {
        baseMapper.batchInsertOnDuplicateKeyIgnore(list);
    }
    @Override
    public IPage<UserInfo> pageWithoutPassword(Page<UserInfo> page, UserInfoVO userInfo) {
        LambdaQueryWrapper<UserInfo> wrapper = new LambdaQueryWrapper<>();
        if (userInfo != null && userInfo.getUsername() != null) {
            wrapper.like(UserInfo::getUsername, userInfo.getUsername());
        }
        if(userInfo != null &&null!=userInfo.getIntimes() && !userInfo.getIntimes().isEmpty()){
            wrapper.between(UserInfo::getIntime, userInfo.getIntimes().get(0), userInfo.getIntimes().get(1) );
        };
        wrapper.select(UserInfo.class, info -> !"password".equals(info.getColumn()));
        return this.page(page, wrapper);
    }
    @Override
    public boolean resetPassword(String id) {
        UserInfo userInfo = this.getById(id);
        if (userInfo == null) {
            return false;
        }


        userInfo.setPassword(PWUtils.encryptPassword(defaultPassword));
        return this.updateById(userInfo);
    }

    @Override
    public boolean updatePassword(String id, String oldPassword, String newPassword) {
        UserInfo userInfo = this.getById(id);
        if (userInfo == null || PWUtils.match(newPassword,userInfo.getPassword())) {
            throw new ServiceException("原密码错误");
        }
        userInfo.setPassword(PWUtils.encryptPassword(newPassword));
        return this.updateById(userInfo);
    }
    @Override
    public boolean isUsernameUnique(String username, String id) {
        QueryWrapper<UserInfo> wrapper = new QueryWrapper<>();
        wrapper.eq("username", username);
        if (id != null) {
            wrapper.ne("id", id);
        }
        return this.count(wrapper) == 0;
    }

    @Override
    public UserInfo getUserByUsername(String username) {
        return baseMapper.selectByUsername(username);
    }
}
